//npm init -y
//npm i --save express
//npm i --save mysql
//node index.js
let express = require('express');
let app = express()
let mysql = require("mysql")
let bodyParser = require('body-parser')
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({
	extended: false
}))
var connection = mysql.createConnection({
	host: "180.97.195.131", //
	port: 3306,
	user: "test2",
	password: "test2",
	database: "test2" //
})

//指定静态资源目录
app.use("/", express.static('public'))
//请求购物车数据接口，登陆后需要根据用户id去请求数据
app.get("/loadGoods", function(req, res) {
	connection.query(`select * from good_list WHERE img!=''`, null, function(err, data) {
		/*console.log(data.length)*/
		if(err){console.log(err)}
		res.json(data)
	})
})
//增添用户地址接口
	app.post("/addAddress", function(req, res) {
					connection.query(`insert into user_address values (id,?,?)`, [
						req.body.address,
						req.body.userid
					], function(err, data) {
						/*console.log(data)*/
						res.json(data)
						/*console.log(data)
						if(data.affectedRows === 1) {
							res.json({
								msg: "新建地址成功"
							})
						} else {
							res.json({
								msg: "新建地址失败"
							})
						}*/
					})
				})
//此处为请求用户地址
app.get("/appendAddress", function(req, res) {
	connection.query(`select * from user_address WHERE userid=?`, [req.query.userid], function(err, data) {
		if(err){console.log(err)}
		/*console.log(data)*/
		res.json(data)
	})
})
//此处为修改用户地址
app.post("/repairAddress", function(req, res) {
	connection.query(`UPDATE user_address SET address=? WHERE userid=?`, [req.body.address,req.body.userid], function(err, data) {
		if(err){console.log(err)}
		/*console.log(data)*/
		res.json(data)
	})
})
//此处为创建订单接口
app.post("/createOrder", function(req, res) {
	connection.query(`INSERT INTO order_user VALUES(id,?,SYSDATE(),?,?,'等待付款',?,?)`, [req.body.count,req.body.orderNumber,req.body.totalPrice,req.body.userid,req.body.goodsid], function(err, data) {
		if(err){console.log(err)}
		console.log(data)
		res.json(data)
	})
})

//商品详情页接口
app.get("/goods",function (req,res) {
	connection.query(`select * from good_list where id = ?`,[req.query.id],function (err,data) {
		if(err) {
			console.log(err)
		} else {
			res.json(data[0])
		}
	})
})
//商品title图接口
app.get("/goods_title_img",function (req,res) {
	connection.query(`select imgadd from goods_img where goodsid = ? and typeid = 1`,[req.query.id],function (err,data) {
		if(err) {
			console.log(err)
		} else {
			console.log(data)
			res.json(data)
		}
	})
})
//商品desc图接口
app.get("/goods_desc_img",function (req,res) {
	connection.query(`select imgadd from goods_img where goodsid = ? and typeid = 2`,[req.query.id],function (err,data) {
		if(err) {
			console.log(err)
		} else {
			res.json(data)
		}
	})
})
//增加喜欢商品
app.post('/goodslikeadd',function (req,res) {
	connection.query(`INSERT INTO goods_favourite VALUES (id,?,?)`,[req.body.userid,req.body.goodsid],function (err,data) {
		if(err) {
			console.log(err)
		} else {
			if(data.affectedRows === 1) {
				res.json({
					msg: "添加成功"
				})
			} else {
				res.json({
					msg: "添加失败"
				})
			}
		}
	})
})
//删除喜欢的商品
app.post('/goodslikedel',function (req,res) {
	connection.query(`DELETE FROM goods_favourite WHERE userid = ? and goodsid = ?)`,[req.body.userid,req.body.goodsid],function (err,data) {
		if(err) {
			console.log(err)
		} else {
			if(data.affectedRows === 1) {
				res.json({
					msg: "删除成功"
				})
			} else {
				res.json({
					msg: "删除失败"
				})
			}
		}
	})
})

//查询喜欢商品
app.get('/goodslike',function(req,res){
	connection.query(`SELECT g.title , g.img , g.unitPrice , g.oldPrice FROM goods_favourite gf 
		INNER JOIN good_list g ON gf.goodsid = g.id where userid=?`,[req.query.userid],function (err,data) {
		if(err) {
			console.log(err)
		} else {
			res.json(data)
		}
	})
})

//此为创建购物车后台接口
app.post("/addshopcarlist", function(req, res) {
	connection.query(`INSERT INTO shopcarlist VALUES(id,?,?,?)`, [req.body.count,req.body.id,req.body.userid], function(err, data) {
		if(err){console.log(err)}
		console.log(data)
		res.json(data)
	})
})

//此为更新后台商品数量接口（用户点击同一商品时）郭新怡
app.post("/updatecount", function(req, res) {
	connection.query(`UPDATE shopcarlist SET count = ?  WHERE goosid=? AND userid = ?`, [req.body.count,req.body.id,req.body.userid], function(err, data) {
		if(err){console.log(err)}
		console.log(data)
		res.json(data)
	})
})
//生成购物车接口郭新怡
app.get("/loadshoplist", function(req, res) {
	connection.query(`SELECT good_list.*,shopcarlist.* FROM good_list INNER JOIN shopcarlist ON good_list.id=shopcarlist.goosid WHERE shopcarlist.userid=?`, [req.query.id], function(err, data) {
		if(err){console.log(err)}
		console.log(data)
		res.json(data)
	})
})
//登陆后请求购物车数据覆盖cookie郭新怡
app.get("/danduloadshoplist", function(req, res) {
	connection.query(`SELECT * FROM shopcarlist where userid=?`, [req.query.id], function(err, data) {
		if(err){console.log(err)}
		console.log(data)
		res.json(data)
	})
})
//删除后台购物车商品接口
app.post("/delshopcarlist", function(req, res) {
	connection.query(`DELETE FROM shopcarlist WHERE goosid=? and userid=?`, [req.body.id,req.body.userid], function(err, data) {
		if(err){console.log(err)}
		console.log(data)
		res.json(data)
	})
})
//清空购物车数据及后台
app.post("/delAllshopcarlist", function(req, res) {
	connection.query(`DELETE FROM shopcarlist WHERE userid=?`, [req.body.id], function(err, data) {
		if(err){console.log(err)}
		console.log(data)
		res.json(data)
	})
})
//注册接口
//注册直接登录接口
app.get("/registerlogin",function (req,res) {
	connection.query(`select * from user_info where user_tel= ?`,[req.query.tel],function (err,data) {
		if(err) {
			console.log(err)
		} else {
			res.json(data)
		}
	})
})

app.post("/register", function(req, res) {
	connection.query(`
		INSERT INTO user_info VALUES (id, ?, ?,user_realname,user_addr,user_sex,user_img,user_edu,user_job,user_birth,user_marriage,user_pay)`, [
		req.body.tel, req.body.pwd], function(err, data) {
		if(err) {
			console.log(err)
		} else {
			if(data.affectedRows === 1) {
				res.json({
					msg: "注册成功"
				})
			} else {
				res.json({
					msg: "注册失败"
				})
			}
		}
	})
})
//手机号重复注册接口
app.get("/register", function(req, res) {
	connection.query(`select count(*) count from user_info where user_tel=?`, [req.query.tel], function(err, data) {
		if(err) {
			console.log(err)
		} else {
			if(data[0].count === 1) {
				console.log(data[0].count)
				res.json({
					success: false
				})
			} else {
				console.log(data[0].count)
				res.json({
					success: true
				})
			}

		}
	})

})
//登录接口
app.post('/login', function(req, res) {
	connection.query(`select * from user_info where user_tel=? and user_pwd=?`, [req.body.tel, req.body.pwd], function(err, data) {
		if(err) {
			console.log(err)
		} else {
				res.json(data)

		}
	})
})
//获取订单接口
app.get('/getOrder', function(req, res) {
	connection.query(`SELECT order_user.*, user_info.user_tel, user_info.user_realname, good_list.img, good_list.title FROM order_user INNER JOIN 
user_info on order_user.userid=user_info.id INNER JOIN good_list ON order_user.goodsid=good_list.id WHERE user_info.id=?`, [req.query.userId], function(err, data) {
		if(err) {
			console.log(err)
		} else {
			console.log(data)
				res.json(data[0])

		}
	})
})

app.get('/getOrderimg', function(req, res) {
	connection.query(`SELECT order_user.*, user_info.user_tel, user_info.user_realname, good_list.img, good_list.title, good_list.unitPrice FROM order_user INNER JOIN
user_info on order_user.userid=user_info.id INNER JOIN good_list ON order_user.goodsid=good_list.id WHERE user_info.id=?`, [req.query.userId], function(err, data) {
		if(err) {
			console.log(err)
		} else {
			console.log(data)
			res.json(data)

		}
	})
})





app.listen(7777, function() {
	console.log("程序已启动，端口号为7777")
})